from netplug import *
from tcp import *
from utils import *
from cmd import *
import information
import friends
import threading
import database
import account


class netServer():
    def __init__(self,netPlugParamsGroup):
        #load netplug from paramgroup
        self._netplugs = []
        for param in netPlugParamsGroup:
            if param.getType() == NETPLUG_TYPE_TCP:
                self._netplugs.append(tcpNetPlug(param,self))
            else:
                warningOutput("netplug type unknown : %s"%(param.getType(),))
    
    def start(self):
        """start the communication server"""
        for netplug in self._netplugs:
            netplug.start()
        
    def stop(self):
        """stop the communication server"""
        for netplug in self._netplugs:
            netplug.stop()
            
    def handleCmd(self,cmd):
        """handle cmd received by a netplug, return a response cmd
        return cmdErr if error or nothing to reply"""
        #check signature
        if not cmd.checkSignature():
            warningOutput("bad signature for command (Type:%d)"%(cmd.getType(),))
            return cmd(CMD_TYPE_ERR)
        if cmd.getType() == CMD_TYPE_GET:
            cmdResp = respCmd()
            cmdResp.setSenderID(account.getMyAccount().getID())
            #check info in base
            info = information.getInformation(cmd.getID(),friends.getGlobalAccount(cmd.getAuthorID()))
            #info = database.getMyDatabase().getInformation(cmd.getID(),cmd.getAuthorID())
            if not info: #if not, send a "not exist" response
                cmdResp.setCodeRep(CMD_RESP_CODE_NE)
                cmdResp.setData('')
                cmdResp.setSignature(account.getMyAccount().signInformation(cmdResp))
                return cmdResp
            #check access rights
            requester = friends.getGlobalAccount(cmd.getSenderID())
            if not info.getFr().isMember(requester): #if not, send a "not available" response
                cmdResp.setCodeRep(CMD_RESP_CODE_NA)
                cmdResp.setData('')
                cmdResp.setSignature(account.getMyAccount().signInformation(cmdResp))
                return cmdResp                
            #build response
            cmdResp.setCodeRep(CMD_RESP_CODE_OK)
            cmdResp.setData(info.dumps())
            cmdResp.setSignature(account.getMyAccount().signInformation(cmdResp))
            return cmdResp
        elif cmd.getType() == CMD_TYPE_GETVER:
            cmdResp = respCmd()
            cmdResp.setSenderID(account.getMyAccount().getID())
            #check info in base
            info = information.getInformation(cmd.getID(),friends.getGlobalAccount(cmd.getAuthorID()))
            #info = database.getMyDatabase().getInformation(cmd.getID(),cmd.getAuthorID())
            if not info: #if not, send a "not exist" response
                cmdResp.setCodeRep(CMD_RESP_CODE_NE)
                cmdResp.setData('')
                cmdResp.setSignature(account.getMyAccount().signInformation(cmdResp))
                return cmdResp
            #check access rights
            requester = friends.getGlobalAccount(cmd.getSenderID())
            if not info.getFr().isMember(requester): #if not, send a "not available" response
                cmdResp.setCodeRep(CMD_RESP_CODE_NA)
                cmdResp.setData('')
                cmdResp.setSignature(account.getMyAccount().signInformation(cmdResp))
                return cmdResp                
            #build response
            cmdResp.setCodeRep(CMD_RESP_CODE_OK)
            cmdResp.setData(info.getDate().isoformat())
            cmdResp.setSignature(account.getMyAccount().signInformation(cmdResp))
            return cmdResp
        else:
            warningOutput("unhandled command (type:%d)"%(cmd.getType(),))
            cmdResp = cmd(CMD_TYPE_ERR)
            cmdResp.setSenderID(account.getMyAccount().getID())
            cmdResp.setSignature(account.getMyAccount().signInformation(cmdResp))
            return cmdResp
        
        
_server = None

def setDefaultNetServer(server):
    """make server as default netserver in use"""
    global _server
    _server = server

def getNetServer():
    """return the current net server in use"""
    global _server
    return _server